package com.laughing.httpserver.handler;

import com.laughing.httpserver.constant.Constant;
import com.laughing.httpserver.entity.Request;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.io.InputStream;
import java.net.Socket;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author laughing.sheng <laughing1917@yeah.net/>
 * @date 2018/1/25
 * @see
 */
@Slf4j
public class RequestHelper {

    public static Request parse(Socket socket) throws Exception {
        InputStream inputStream=socket.getInputStream();
        Request request = new Request();
        byte[] bytes = new byte[2048];
        int i = inputStream.read(bytes);
        StringBuilder stringBuilder = new StringBuilder();
        for (int j = 0; j < i; j++) {
            stringBuilder.append((char) bytes[j]);
        }
        String requestLine = stringBuilder.toString();
        List<String> requestList = Arrays.asList(requestLine.split("\r\n"));
        String method = getRequestMethod(requestList);
        if (Constant.REQUEST_METHOD_GET.equals(method)) {
            parseHttpGetRequest(request, requestList);
        } else if (Constant.REQUEST_METHOD_POST.equals(method)) {
            parseHttpPostRequest(request, requestList);
        }
        return request;
    }

    private static void parseHttpPostRequest(final Request request, List<String> requestList) {

    }

    private static void parseHttpGetRequest(final Request request, List<String> requestList) {
        request.setMethod(Constant.REQUEST_METHOD_GET);
        if (!CollectionUtils.isEmpty(requestList)) {
            String firstLine = requestList.get(0);
            int lastIndex = firstLine.lastIndexOf("HTTP/1.1");
            String temp = firstLine.substring(4, lastIndex - 1);
            int firstQuestionMarkIndex = temp.indexOf("?");
            if (firstQuestionMarkIndex != -1) {
                String router = temp.substring(0, firstQuestionMarkIndex);
                request.setRequestUrl(router);
                //不要问号
                String param = temp.substring(firstQuestionMarkIndex + 1, temp.length());
                Map<String, String> paramMap = new HashMap<>();
                String[] paramArray = param.split("&");
                for (String s : paramArray) {
                    String[] singleParam = s.split("=");
                    paramMap.put(singleParam[0], singleParam[1]);
                }
                request.setParameterMap(paramMap);
            }else {
                request.setRequestUrl(temp);
            }
        }
    }

    private static String getRequestMethod(List<String> requestList) {
        if (!CollectionUtils.isEmpty(requestList)) {
            String firstLine = requestList.get(0);
            if (firstLine.startsWith("GET")) {
                return "GET";
            } else if (firstLine.startsWith("POST")) {
                return "POST";
            }
        }
        return null;
    }
}
